package com.morningstar.qs.api.basic.common.encrypt.des;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;

import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec; 

public class GenKey {
	private static final String DES = "DES";
	public static final String SKEY_NAME="key.des";
	public static final String PASSWORD_CRYPT_KEY = "Morningstar.Markets";
	public static SecretKey genKey(){
        SecretKey securekey = null;
        try{
        	DESKeySpec dks = new DESKeySpec(PASSWORD_CRYPT_KEY.getBytes());
        	SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES);
        	securekey = keyFactory.generateSecret(dks);  
        }catch(NoSuchAlgorithmException e){
			e.printStackTrace();
		}catch(Exception e){
			e.printStackTrace();
		}
        return securekey;
	}
	public static SecretKey genKey(String path){
		SecretKey key = null;
		if(!existSecDesFile(path)){
			genKeyToFile(path);
		}
		try{
			ObjectInputStream keyFile = new ObjectInputStream(new FileInputStream(genFile(path)));
			key = (SecretKey) keyFile.readObject();
			keyFile.close();
		}catch(FileNotFoundException e){
			throw new RuntimeException(e);
		}catch(Exception e){
			throw new RuntimeException();
		}
		return key;
	}
	public static void genKeyToFile(String path){
		//secretkey
		SecretKey skey=null;
		//random of secretkey
		SecureRandom sr = new SecureRandom();
		//gen the file
		File file = genFile(path);
		try{
			//get the instance of secretkey
			KeyGenerator gen = KeyGenerator.getInstance(DES);
			//init the instance
			gen.init(sr);
			//gen the secretkey
			skey=gen.generateKey();
			//write output scream
			ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(file));
			oos.writeObject(skey);
			oos.close();
		}catch(NoSuchAlgorithmException e){
			e.printStackTrace();
		}catch(FileNotFoundException e){
			e.printStackTrace();
		}catch(IOException e){
			e.printStackTrace();
		}
	}
	private static Boolean existSecDesFile(String path){
		String temp=null;
		File newfile=null;
		if(path.endsWith("/")||path.endsWith("\\")){
			temp=path;
		}else{
			temp=path+"/";
		}
		
		File pathFile = new File(temp);
		if(!pathFile.exists()){
			return false;
		}
		newfile = new File(temp+SKEY_NAME);
		return newfile.exists();
	}
	private static File genFile(String path){
		String temp=null;
		File newfile=null;
		if(path.endsWith("/")||path.endsWith("\\")){
			temp=path;
		}else{
			temp=path+"/";
		}
		
		File pathFile = new File(temp);
		if(!pathFile.exists()){
			pathFile.mkdirs();
		}
		
		newfile = new File(temp+SKEY_NAME);
		return newfile;
	}
}
